﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Buccaneer
{
    public partial class addressbar : UserControl
    {
        public addressbar()
        {
            InitializeComponent();
        }

        #region private attributes

        private BitmapButton[] separators;
        private Label[] textLabels;
        private int selectedItem = 0;
        private string pathSeparator = "\\";
        public event EventHandler OnPressEnter;
        
        #endregion

        #region private functions

        private void addSeparator()
        {
            BitmapButton bitmapButton = new BitmapButton();
            BitmapButtonsInitializer.setFlatAppearance(ref bitmapButton);
            BitmapButtonsInitializer.loadImage(ref bitmapButton, "addressBarSeparatorNormal");
            
            if (separators == null)
            {
                separators = new BitmapButton[1];
                bitmapButton.Location = new Point(textLabels[textLabels.Length - 1].Width - 38, 0);
            }
            else
            {
                Array.Resize<BitmapButton>(ref separators, separators.Length + 1);
                bitmapButton.Location = new Point(10 + textLabels[textLabels.Length - 1].Width + textLabels[textLabels.Length - 1].Location.X, 0);
            }
            bitmapButton.Size = new Size(38, 34);
            bitmapButton.Location = new Point(tbx_addressInput.Location.X - 7 - 38, 0);

            separators[separators.Length - 1] = bitmapButton;

            pnl_everything.Controls.Clear();
            pnl_everything.Controls.AddRange(textLabels);
            pnl_everything.Controls.AddRange(separators);
        }

        private void addLabel(string text)
        {
            Label label = new Label();
            label.Text = text;
            label.AutoSize = true;
            label.Font = new Font("Arial", 12f, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            
            if (textLabels == null)
            {
                textLabels = new Label[1];
            }
            else
            {
                Array.Resize<Label>(ref textLabels, textLabels.Length + 1);
            }

            label.Location = new Point(tbx_addressInput.Location.X - 7, 8);

            label.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pnl_everything_MouseClick);

            textLabels[textLabels.Length - 1] = label;

            pnl_everything.Controls.AddRange(textLabels);
            if (separators != null)
                pnl_everything.Controls.AddRange(separators);
        }

        private bool deleteLastItem()
        {
            bool retval = true;
            pnl_everything.Controls.Clear();
            if (textLabels != null && textLabels.Length > 0)
            {
                Array.Resize<Label>(ref textLabels, textLabels.Length - 1);
                pnl_everything.Controls.AddRange(textLabels);
            }
            else retval = false;
            if (separators != null && separators.Length > 0)
            {
                Array.Resize<BitmapButton>(ref separators, separators.Length - 1);
                pnl_everything.Controls.AddRange(separators);
            }
            else retval = false;
            if (textLabels != null && textLabels.Length == 0) retval = false;
            return retval;
        }

        private void addressbar_Load(object sender, EventArgs e)
        {
            pnl_everything.Width = Width - 13;
        }

        private void tbx_addressInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (tbx_addressInput.Text != "")
                {
                    addLabel(tbx_addressInput.Text);
                    tbx_addressInput.Location = new Point(10 + textLabels[textLabels.Length - 1].Width + textLabels[textLabels.Length - 1].Location.X + 36, 10);
                    addSeparator();
                    tbx_addressInput.Text = "";
                }
                OnPressEnter.Invoke(this, EventArgs.Empty);
            }
            else if (e.KeyData == Keys.Back && tbx_addressInput.Text == "")
            {
                try { tbx_addressInput.Text = textLabels[textLabels.Length - 1].Text + "x"; }
                catch (Exception) { }

                if (deleteLastItem())
                {
                    tbx_addressInput.Location = new Point(10 + textLabels[textLabels.Length - 1].Width + textLabels[textLabels.Length - 1].Location.X + 36, 10);
                }
                else
                {
                    tbx_addressInput.Location = new Point(10, 10);
                }
                tbx_addressInput.Select(tbx_addressInput.Text.Length, 0);
            }
        }

        private void tbx_addressInput_TextChanged(object sender, EventArgs e)
        {
            if (tbx_addressInput.Text.Length > pathSeparator.Length && tbx_addressInput.Text.Substring(tbx_addressInput.Text.Length - pathSeparator.Length, pathSeparator.Length) == pathSeparator)
            {
                addLabel(tbx_addressInput.Text.Substring(0, tbx_addressInput.Text.Length - 1));
                tbx_addressInput.Location = new Point(10 + textLabels[textLabels.Length - 1].Width + textLabels[textLabels.Length - 1].Location.X + 36, 10);
                tbx_addressInput.Text = "";
                addSeparator();
            }
            Graphics g = this.CreateGraphics();
            tbx_addressInput.Size = new Size((int)(g.MeasureString(tbx_addressInput.Text, tbx_addressInput.Font).Width) + 30, 10);
        }

        private void pnl_everything_MouseClick(object sender, MouseEventArgs e)
        {
            tbx_addressInput.Focus();
            if (separators != null && separators.Length > 0)
            {
                for (int i = separators.Length - 1; i >= 0; i--)
                {
                    if (e.X > separators[i].Location.X) break;
                    else
                    {
                        try { tbx_addressInput.Text = textLabels[textLabels.Length - 1].Text; }
                        catch (Exception) { }

                        if (deleteLastItem())
                        {
                            tbx_addressInput.Location = new Point(10 + textLabels[textLabels.Length - 1].Width + textLabels[textLabels.Length - 1].Location.X + 36, 10);
                        }
                        else
                        {
                            tbx_addressInput.Location = new Point(10, 10);
                        }
                        tbx_addressInput.Select(tbx_addressInput.Text.Length, 0);
                    }
                }
            }
        }

        #endregion

        #region public attributes

        public string Text
        {
            get
            {
                string retval = "";
                for (int i = 0; i < textLabels.Length; i++)
                {
                    retval += textLabels[i].Text + pathSeparator;
                }
                return retval;
            }
        }

        public string PathSeparator
        {
            get { return pathSeparator; }
            set { pathSeparator = value; }
        }

        #endregion

        #region public functions



        #endregion
    }

}